/**
 * This software was produced for the U. S. Government
 * under Contract No. FA8721-09-C-0002, and is
 * subject to the Rights in Noncommercial Computer Software
 * and Noncommercial Computer Software Documentation Clause
 * (DFARS) 252.227-7014 (JUN 1995)
 * 
 * (c) 2009 The MITRE Corporation. All Rights Reserved.
 *
 * SPMIF to HYPERFORMIX Translator
 * Author: EWALTERS
 * Created: 1:25 PM, Jan 13, 2009
 * MITRE Corporation
 */

package com.mitre.spmif2hyperformix;

import com.mitre.spmif2hyperformix.Options;
import com.mitre.spmif2hyperformix.amgxml.HyperformixXMLConverter;
import com.mitre.spmif2hyperformix.csv.HyperformixCSVConverter;
import com.mitre.spmif2hyperformix.hyperformix.importexport.Model;
import com.mitre.spmif2opnet.XMLInterface;
import com.mitre.spmif2opnet.analysis.AnalysisContext;
import com.mitre.spmif2opnet.analysis.GraphBuilder;
import com.mitre.spmif2opnet.analysis.Priorities.PriorityType;
import com.mitre.spmif2opnet.generation.OpnetGraphConverter;
import com.mitre.spmif2opnet.spmif.ProjectType;

/**
 * Top-level class for spmif2hyperformix.  It is not really spmif2hyperformix, really
 *  much more like spmif2opnet2hyperformix, since the task-job-application paradigm
 *  initally seemed to carry over.  Results are mixed.
 */
public class spmif2hyperformix {

	/**
	 * Main entry point
	 * @param args - command-line arguments
	 */
	public static void main(String[] args) {
		// Grab the options from the CLI
		Options opts = new Options();
		if (!opts.getOptions("spmif2hyperformix", args)) {
			opts.usage();
			System.exit(-1);
		}
		
		try {
			
			// Read in the XML-based representation
			XMLInterface xml_io = new XMLInterface();
			ProjectType project = xml_io.loadXMLFile(opts.getInputFileName());
			
			// Convert the flat XML-based representation into a series of 
			//  fully resolved adjacency lists, as we generally can't do cross-task 
			//  references or nested task elements
			System.out.println("[INFO] Creating operation graph from S-PMIF...");
			GraphBuilder gbuilder = new GraphBuilder();
			// This is a version of the context for Hyperformix with some default values for Priorities and Properties 
			AnalysisContext a_context = new AnalysisContext(PriorityType.RMA_PRIORITY, opts.getProperties());
			a_context.setFileName(opts.getInputFileName());
			// Projects stored in the context
			gbuilder.buildGraph(project, a_context);
			
			// Resolve all of the variables
			a_context.resolveVars();
			
			// After this, we should have all of the projects/scenarios we are going to need, 
			//  and all of the variables will be resolved
						
			// Determine how best to map these graphs onto an OPNET-style network
			System.out.println("[INFO] Inferring HyPerformix data from operation graph...");
			OpnetGraphConverter conv = new OpnetGraphConverter();
			conv.convert(a_context);

			// Output the OPNET network into a HyPerformix format
			if (opts.getOutputFormat().equals(Options.HyperformixOutputFormat.CSV_FILE)) {
				// Comma-separated TSUMM
				HyperformixCSVConverter csv_conv = new HyperformixCSVConverter();
				csv_conv.convert(opts, conv.getJobs(), conv.getTasks(), conv.getLinks());
			} else if (opts.getOutputFormat().equals(Options.HyperformixOutputFormat.XML_FILE)) {
				// HyPerformix XML
				HyperformixXMLConverter xml_conv = new HyperformixXMLConverter();
				Model model = xml_conv.convert(opts, conv.getJobs(), conv.getTasks(), conv.getLinks(), conv.getServers());
				xml_conv.exportXMLFile(opts.getOutputFileName(), model);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(-1);
		}
	
	}
}
